package controller

import (
	"gitee.com/molonglove/goboot/gin"
	"helper-api/app/constant"
	"helper-api/app/models/request"
	"helper-api/app/models/response"
	"helper-api/app/service"
	"helper-api/utils"
	"net/http"
)

var Role = new(RoleController)

type RoleController struct{}

// CreateRole 创建角色
func (r *RoleController) CreateRole(c *gin.Context) {
	var (
		customErr *response.BusinessError
		result    *response.RoleListResponse
		param     request.RoleCreateRequest
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Role.CreateRole(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// UpdateRole 更新角色
func (r *RoleController) UpdateRole(c *gin.Context) {
	var (
		customErr *response.BusinessError
		result    *response.RoleListResponse
		param     request.RoleUpdateRequest
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Role.UpdateRole(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// DeleteRole 删除角色
func (r *RoleController) DeleteRole(c *gin.Context) {
	var (
		userId    int64
		param     string
		ids       []int64
		customErr *response.BusinessError
	)
	if param = c.DefaultQuery("ids", ""); param == "" {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if ids = utils.StrToArray(param); len(ids) <= 0 {
		c.JSON(http.StatusOK, response.Ok("暂无数据需要操作"))
		return
	}
	userId = c.GetInt64(constant.HeaderUserKey)
	if customErr = service.Role.DeleteRole(ids, userId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(response.Menus[response.DeleteSuccess]))
}

// PageRole 角色分页
func (r *RoleController) PageRole(c *gin.Context) {
	var (
		page      *response.PageData
		customErr *response.BusinessError
		param     request.RolePageRequest
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if page, customErr = service.Role.PageRole(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(page))
}

// RoleAll 获取所有角色
func (r *RoleController) RoleAll(c *gin.Context) {
	var (
		result    []response.RoleKeyValueResponse
		customErr *response.BusinessError
	)
	if result, customErr = service.Role.RoleAll(); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}
